var Modal;

/*
	Core Functions
*/

function Init_zebra() {
	
	$$('tr:even').each(function(tr) {
		
		if (tr.getElement('th') == null) {
				
				tr.addClass("even");
		}
	});
	
	$$('tr:odd').each(function(tr) {
		
		if (tr.getElement('th') == null) {
				
				tr.addClass("odd");
		}
	});
}

function Init_tabs() {
	
	document.getElements('div[id=tab]').each(function(element) {
		
		new SimpleTabs(element, {
			selector: 'h4'
		});
	});
}

function InitAll() {
	
	Init_zebra();
}

function BoxFolding(id) {
	
	var BoxSlide = new Fx.Slide(id);
	
	BoxSlide.toggle();
}

function ajax_url(url, id) {
		
	var EI = $(id);
	var Loader = $('ajax-loader');
	
	Loader.removeClass('hide');
	
	var ajax = new Request.HTML({
		evalScripts: false,
		url : url, 
		onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript) {
			
			EI.empty();
			EI.adopt(responseTree);
			eval(responseJavaScript);
			InitAll();
			Loader.addClass('hide');
		}
	});
	
	ajax.send();
}

function ajax_form(id, contaner, uri) {
	
	var id = $(id);
	var contaner = $(contaner);

	id.set('send', {
		url : uri,
		onComplete: function(response) {
		
			contaner.set('html', response);
	}});
	
	id.send();
}

function ajax_post(url, contener, post) {

    var EI = $(contener);
    var Loader = $('ajax-loader');
    
    Loader.removeClass('hide');

    var Req = new Request({

        url: url,
        data: post,
        onSuccess: function(txt){
        
    		EI.set('html', txt);

            Loader.addClass('hide');
        }

    });
    
    Req.send();
}

function ajax() {
	
	
}

function ModalBox(url, width, title) {
	
	Modal.alert(url, title);
}

function ModalBoxQuestion(url, title){
	
	var cont = '<center><button onclick="window.location = \'' +url + '\';">Ок</button> <button onclick="Modal.close()">Отмена</button></center>';
	
	Modal.alertText(cont, title);
}

function ModalBoxText(text, title) {
	
	Modal.alertText(text, title);
}

function ModalBoxClose() {
	
	Modal.close();
}

/*
	Admin JavaScript
*/

window.addEvent('domready', function() {

	Modal = new ModalBoxClass('ajax-loader');
	
	//Folding widgets
	Boxes = document.getElements('div[class=box]');
	Boxes.each(function(element) {
		
		var BoxSlide = new Fx.Slide(element.getElement('div[class=box_content]'), {duration: 400});
		var BoxLink = element.getElement('h3').getElement('a');
		var BoxLinkPos = element.getElement('div[class=box_content]').get('text').length;
		
		BoxLink.addEvent('click', function(e){
			
			e.stop();
			BoxSlide.toggle();
			
			if (BoxSlide.open) {
				
				Cookie.write('Box_'+BoxLinkPos, 0, {path: '/'});
			}
			else {
				
				Cookie.write('Box_'+BoxLinkPos, 1, {path: '/'});
			}
			
		});
		
		if (Cookie.read('Box_'+BoxLinkPos) == 0) {
			
			BoxSlide.hide();
		}
		
	});
	
	//Zebra table	
	Init_zebra();
	
	//Tabs
	Init_tabs();
	
	//Tips
	new Tips(document.getElements('span[class=tip]'));
	
});

function widget_doubl_input(id) {
	
	id = $(id);
	
	var Parent = id.getParent();
	var New = Parent.clone(true, true);
	
	New.injectAfter(Parent);
	
	New.getChildren().each(function(EL) {
		
		if (EL.nodeName == "INPUT") {
		
			EL.set('value', '');
		}
		else if(EL.nodeName == "A" && EL.get('id') != 'doublwmov') {
			
			EL.set('id', EL.get('id') + $random(1, 10000));
		}
		
	});
	
	widget_doubl_input_mov();
}

function widget_doubl_input_del(id) {
	
	$(id).getParent().dispose();
	widget_doubl_input_mov();
}

function widget_doubl_input_mov() {
	
	var List = new Sortables($('widget_list_menu'), {
		handle: 'a[id=doublwmov]'
	});
}
